<?php	
	/*
	 *	Sample Module template for "OH"
	 *  
	 *  use for write develop module interface
	 *  to encampusulate opensource script into "OH"
	 */
	
	// Interface function	
	function oh_module_lang($tokens) {
		global $oh_object_lang;
		return $oh_object_lang;	
	} 

	// Option file required
	//require_once($oh['path']."/modules/mail/libmail.php");
	
	// Main class of module
	class oh_class_lang {				
		public $locale = "EN";		
		function oh_class_lang() {
		}
		function init($file,$locale) {
			file_put_contents(
				$file,
				'<?php /* Language definition constants */'."\n".
				'/* start definitions */'."\n".
				'define("OH_LANG_'.$locale.'","'.$file.'");'."\n".				
				'/* end definitions */'."\n".
				'?>'
			);
		}
		function locale($locale) {
			$locale = $this->key($locale);
			$locale_file = oh("path.script")."/lang/".$locale.".php";
			if (!file_exists($locale_file)) {
				$this->init($locale_file,$locale);
			}
			require_once($locale_file);
			$this->locale = $locale;
		}
		function key($s) {
			return trim(strtoupper(preg_replace("/[^a-zA-Z]/","_",$s)));
		}
		function add($file,$locale,$id,$text) {			
			if (!file_exists($file)) $this->init($file,$locale);
			require_once($file);
			if (!defined($id)) {
				$def = file_get_contents($file);			
				$def = str_replace(
					'/* end definitions */',
					'define("'.$id.'","'.$text.'");'."\n".'/* end definitions */',
					$def
				);
				file_put_contents($file,$def);
			}
		}
		function define($locale1,$text1,$locale2,$text2,$src="") {
			$locale1 	= $this->key($locale1);
			$text1_id 	= $this->key($text1);
			$locale2 	= $this->key($locale2);
			$text2_id 	= $this->key($text2);

			$key_text1 	= 'OH_LANG_'.$locale2.'_'.$text1_id;
			$loc_file1 	= oh("path.script")."/lang/".$locale1.".php";
			$key_text2 	= 'OH_LANG_'.$locale1.'_'.$text2_id;
			$loc_file2 	= oh("path.script")."/lang/".$locale2.".php";
			
			//$this->add($loc_file1,$text1_id,$text1);
			$this->add($loc_file1,$locale1,$key_text2,$text1);
			$this->add($loc_file2,$locale2,$key_text1,$text2);
			//$this->add($loc_file2,$text2_id,$text2);
		}
		function translate($text) {
			$id = 'OH_LANG_'.$this->locale.'_'.$this->key($text);
			if (defined($id)) {
				return constant($id);
			} else {
				return $text;
			}
		}
	}
	
	// Main object of module
	global $oh_object_lang;
	$oh_object_lang = new oh_class_lang();
	
	function i($text){
		global $oh_object_lang;
		return $oh_object_lang->translate($text);
	}
?>
